package aaaClicker;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;

import org.junit.*;

import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;

import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.Select;

import com.attilax.core;
import com.attilax.io.filex;
import com.attilax.io.pathx;
import com.thoughtworks.selenium.Selenium;
import com.thoughtworks.selenium.webdriven.WebDriverBackedSelenium;

public class SelemAti_backExe {

	private boolean acceptNextAlert = true;
	private StringBuffer verificationErrors = new StringBuffer();

	// private RemoteWebDriver driver;

	public static void main(String[] args) {
		// 59.58.162.141:888 111.12.251.199:80
		Map mp = new HashMap();
		final List<String> li = filex
				.read2list_filtEmptyNstartSpace("e:\\proxy.txt");
		for (int i = 0; i < 10037; i++) {
			final int j = i;
			core.execMeth_Ays(new Runnable() {

				@Override
				public void run() {
					single(li, j);

				}
			}, "threadName" + String.valueOf(i));
			try {
				Thread.sleep(15000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
		System.out.println("--f");
		while (true) {
			try {
				Thread.sleep(5000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		// System.out.println("--f");

	}

	private static void single(List<String> li, int i) {
		WebDriver driver =new HtmlUnitDriver();
		//Selenium selenium = null;
		
		try {
			System.out.println("---cur start:" + String.valueOf(i));
			FirefoxProfile profile = new FirefoxProfile();
			try {
				// 使用代理
				profile.setPreference("network.proxy.type", 1);
				// http协议代理配置
				String[] prx = li.get(i).trim().split(":");
				profile.setPreference("network.proxy.http", prx[0].trim());
				profile.setPreference("network.proxy.http_port",
						Integer.parseInt(prx[1].trim()));
				// 对于localhost的不用代理，这里必须要配置，否则无法和webdriver通讯
				profile.setPreference("network.proxy.no_proxies_on", "localhost");

				Proxy proxy = new Proxy(); // jeig proxy only for ie
			} catch (Exception e) {
				// TODO: handle exception
			}
			
			// String proxyStr=li.get(i);
			// proxy.setHttpProxy(proxyStr);
			// profile.set
			// seleniu
			
			// 第三种全部删除
			driver.manage().deleteAllCookies();
			// driver.d
			// WebDriver driver;
			String baseUrl;
			// driver = new FirefoxDriver();
			baseUrl = "http://www.baidu.com/baidu?wd=ip+%B5%D8%D6%B7&tn=monline_4_dg";
			baseUrl = "http://guang.zhe800.com/guangbaobao";
//			selenium = new WebDriverBackedSelenium(driver, baseUrl);
//			selenium.deleteAllVisibleCookies();
			driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
		 
		 	driver.get(baseUrl + "");
			// 定位到所有<input>标签的元素，然后输出他们的id
			System.out.println("scan a start" + String.valueOf(i));
	 	   driver.findElement(By.linkText("百搭欧简约时尚单肩大包手提包包")).click();
//			List<WebElement> element = driver.findElements(By.tagName("a"));

//			for (WebElement e : element) {
//				System.out.println(e.toString());
//				if (e.getAttribute("href").startsWith(
//						"http://out.zhe800.com/guang/p/deal/100040878")) {
//					System.out.println(e.getAttribute("href"));
//					e.click();
	//
//					break;
//				}
	//
//			}
			System.out.println("scan a end" + String.valueOf(i));
			// String js = filex
			// .read(pathx.classPath(ClickX.class) + "\\login.js");
			// ((JavascriptExecutor) driver).executeScript(js);
			// 第三种全部删除
			// driver.manage().deleteAllCookies();
			try {
				Thread.sleep(30000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		finally{
			try {
				driver.quit();
				
			} catch (Exception e2) {
				// TODO: handle exception
			}
//			try {
//				selenium.stop();
//			} catch (Exception e2) {
//				// TODO: handle exception
//			}	
			System.out.println("---cur finish:" + String.valueOf(i));
		}
		
	}

	@Before
	public void setUp() throws Exception {

	}

	@Test
	public void testSelemAti() throws Exception {

		// driver.findElement(By.id("kw")).click();
		// driver.findElement(By.id("kw")).clear();
		// driver.findElement(By.id("kw")).sendKeys("attilax");
		// driver.findElement(By.id("su")).click();
	}

	// @ After
	public void tearDown() throws Exception {
		// driver.quit();
		String verificationErrorString = verificationErrors.toString();
		if (!"".equals(verificationErrorString)) {
			fail(verificationErrorString);
		}
	}

	private boolean isElementPresent(By by) {
		try {
			// driver.findElement(by);
			return true;
		} catch (NoSuchElementException e) {
			return false;
		}
	}

	private boolean isAlertPresent() {
		try {
			// driver.switchTo().alert();
			return true;
		} catch (NoAlertPresentException e) {
			return false;
		}
	}

	private String closeAlertAndGetItsText() {
		return null;
		// try {
		// Alert alert = driver.switchTo().alert();
		// String alertText = alert.getText();
		// if (acceptNextAlert) {
		// alert.accept();
		// } else {
		// alert.dismiss();
		// }
		// return alertText;
		// } finally {
		// acceptNextAlert = true;
		// }
	}
}
